/* By Julien CORON @ 2011
 * 
 */


var configuration = {
	holderWidth : 640,
	holderHeight : 480,
	lineOffsetX : 10,
	lineOffsetY : 20,
	headerTextArray : ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi"],
	headerHeight : 40,
	headerFontSize : 15 /*px*/,
	hourHeight : 40,
	hoursArray : ["10","11","12","13","14","15","16","17","18","19","20","21","22"],
	hoursWidth : 50,
	hoursFontSize : 10,
	hourSeparators : "h"
};
configuration.nbCols = configuration.headerTextArray.length;
configuration.colWidth = (configuration.holderWidth - configuration.lineOffsetX - configuration.lineOffsetX - configuration.hoursWidth) / configuration.nbCols;

function preparePaper(divId) {

	var paper = Raphael(divId, configuration.holderWidth, configuration.holderHeight);


	// DESSIN DU CALNDRIER VIDE
	// bordure
    paper.rect(1,1, configuration.holderWidth-1, configuration.holderHeight-1,10).attr({fill: "#ccc", stroke: "#000", opacity: 1});

	// colonnes
	for(var i=0; i<configuration.nbCols; i++) {
		paper.path("M "+ (configuration.hoursWidth+configuration.lineOffsetX + (i*configuration.colWidth)) +" " + configuration.lineOffsetY + " L "+ (configuration.hoursWidth+configuration.lineOffsetX + (i*configuration.colWidth)) +" "+ (configuration.holderHeight - configuration.lineOffsetY) ).attr({stroke: "#aaa"});
		// texte centre par defaut
		var textObj = paper.text( configuration.hoursWidth+configuration.lineOffsetX + (i*configuration.colWidth) + (configuration.colWidth/2) , configuration.headerHeight - configuration.headerFontSize, configuration.headerTextArray[i]).attr({font: configuration.headerFontSize + 'px "Tahoma"'});
		textObj.attr({fill: "#000"});
		//DEBUG
		//paper.circle( configuration.hoursWidth+configuration.lineOffsetX + (i*configuration.colWidth) , configuration.headerHeight - 10, 2).attr({fill: "#f00"});

	}
	// derniere ligne
	paper.path("M "+ (configuration.hoursWidth+configuration.lineOffsetX + (configuration.nbCols*configuration.colWidth)) +" " + configuration.lineOffsetY + " L "+ (configuration.hoursWidth+configuration.lineOffsetX + (configuration.nbCols*configuration.colWidth)) +" "+ (configuration.holderHeight - configuration.lineOffsetY) ).attr({stroke: "#aaa"});
	
	// ligne horizontale fin d'entete
	var ligneH = paper.path("M "+configuration.lineOffsetX+" "+configuration.headerHeight+" L "+(configuration.holderWidth-configuration.lineOffsetX)+" "+configuration.headerHeight );
	ligneH.attr({stroke: "#000"});

	// lignes horizontales
	for(var i=0;i<configuration.hoursArray.length;i++) {
		var ligneH1 = paper.path("M "+configuration.lineOffsetX+" "+(configuration.headerHeight + (i*configuration.hourHeight))+" L "+(configuration.holderWidth-configuration.lineOffsetX)+" "+(configuration.headerHeight + (i*configuration.hourHeight)) );
		ligneH1.attr({stroke: "#999"});
		
		var textObj = paper.text( configuration.lineOffsetX + 5 , (configuration.headerHeight + (i*configuration.hourHeight)) + (configuration.hoursFontSize/2), configuration.hoursArray[i] + configuration.hourSeparators + "00").attr({font: configuration.hoursFontSize + 'px "Arial"',"text-anchor": "start"});
		textObj.attr({fill: "#555"});
		//DEBUG
		//paper.circle( configuration.lineOffsetX + 5 , (configuration.headerHeight + (i*configuration.hourHeight))+ configuration.hoursFontSize, 2).attr({fill: "#f00"});
	}
	// derniere ligne
	//var ligneH1 = paper.path("M "+configuration.lineOffsetX+" "+(configuration.headerHeight + (configuration.hoursArray.length*configuration.hourHeight))+" L "+(configuration.holderWidth-configuration.lineOffsetX)+" "+(configuration.headerHeight + (configuration.hoursArray.length*configuration.hourHeight)) );
	//ligneH1.attr({stroke: "#999"});
	

	/* exemples de réservations
	var resa1 = drawReservation(paper,"Mardi","10h00","11h00").attr({fill: "#22f", stroke: "#000", opacity: 0.8});
	var textObj1 = paper.text( resa1.getBBox().x + (resa1.getBBox().width/2), 
			resa1.getBBox().y + (resa1.getBBox().height/2), 
			"Reservation").attr({font: configuration.hoursFontSize + 'px "Arial"'});
	textObj1.attr({fill: "#000"});
	
	

	var resa = drawReservation(paper,"Vendredi","12h30","14h15").attr(
			{fill: "#bebeff", stroke: "#444", opacity: 1}
		);
	paper.rect(resa.getBBox().x+1, resa.getBBox().y+1, resa.getBBox().width-2, resa.getBBox().height-2, 4).attr(
			{stroke: "none", gradient: "270-rgba(255, 255, 255, 0)-rgba(200, 200, 255, 1):75",opacity: 0}
		);

	var textObj = paper.text( resa.getBBox().x + (resa.getBBox().width/2),
			resa.getBBox().y + (resa.getBBox().height/2),
			"Reservation").attr({font: configuration.hoursFontSize + 'px "Arial"'});
	textObj.attr({fill: "#000"});
*/
	return paper;
};

function drawReservation(paper, reservationDay, reservationStart, reservationStop){

	var days = configuration.headerTextArray.join("-").toUpperCase().split( new RegExp("[-]+","g") );
	var dayNum = days.indexOf(reservationDay.toUpperCase());
	var reg = new RegExp("(..)"+configuration.hourSeparators+"(..){0,1}", "g");

	var hoursAndMinutesStart = reservationStart.split(reg);
	var hoursAndMinutesStop = reservationStop.split(reg);

	var hourNum = configuration.hoursArray.indexOf(hoursAndMinutesStart[1]) + (hoursAndMinutesStart[2] / 60);
	var hourAmount = configuration.hoursArray.indexOf(hoursAndMinutesStop[1]) - hourNum + (hoursAndMinutesStop[2] / 60) ;
	
	var reservationX = (configuration.hoursWidth+configuration.lineOffsetX + (dayNum*configuration.colWidth));
	var reservationY = (configuration.headerHeight + (hourNum*configuration.hourHeight));
	var reservationHeight = hourAmount*configuration.hourHeight;
	reservation = paper.rect(reservationX, reservationY, configuration.colWidth - 10, reservationHeight, 4);
	// DEBUG
	// TOP-LEFT corner
	//paper.circle(resa.getBBox().x, resa.getBBox().y, 2).attr({fill: "#f00"});
	// CENTER
	//paper.circle(resa.getBBox().x + (resa.getBBox().width/2), resa.getBBox().y + (resa.getBBox().height/2), 2).attr({fill: "#f00"});

	return reservation;
}
